import pyodbc


def execute_sql_statement(sql_statement, connection_dict):
    """
    Executes an SQL statement and returns the results.

    :param sql_statement: The SQL statement to execute.
    :param connection_dict: A dictionary containing the connection details for the database.
    :return: A list of tuples containing the query results.
    """
    # Construct the connection string from the dictionary
    connection_string = (
        f"DRIVER={{{connection_dict['driver']}}};"
        f"SERVER={connection_dict['server']};"
        f"DATABASE={connection_dict['database']};"
        f"UID={connection_dict['username']};"
        f"PWD={connection_dict['password']};"
    )

    # Connect to the database using the connection string
    conn = pyodbc.connect(connection_string)

    # Create a cursor object to execute the SQL statement
    cursor = conn.cursor()

    # Execute the SQL statement
    cursor.execute(sql_statement)

    # Fetch the query results
    query_results = cursor.fetchall()

    # Close the cursor and connection
    cursor.close()
    conn.close()

    # Return the query results
    return query_results


connection_dict = {
    "driver": "SQL Server",
    "server": "localhost",
    # "server": "127.0.0.1",
    "database": "xhedu",
    "username": "xhedu",
    "password": "123456",
}

stmt = "SELECT * FROM edu_class"

result = execute_sql_statement(stmt, connection_dict)

for r in result:
    print(r)
